#!C:\Python26

import re


def updateGUI(mainptr, configDict):

	mainptr.Freeze()								# Freeze gui while modifying it

	if not configDict["artistListEN"]:						# Enable or Disable List Input Field
		mainptr.aListField.SetValue("Disabled")
		mainptr.aListField.SetEditable(0)
		mainptr.aListField.Disable()
	else:
		mainptr.aListField.SetEditable(1)
		mainptr.aListField.SetValue("")
		mainptr.aListField.Enable()



	mainptr.nameSourcesSet.Clear()							# Clear and Repopulate Namesource Dropbox

	print "nameSource len", len(configDict["nameSource"])
	print configDict["nameSource"]

	if len(configDict["nameSource"]) > 0 and configDict["pointer"].version > 1.2:

		mainptr.nameSourcesSet.Enable()

		mainptr.nameSourcesSet.SetValue("")
		for option in configDict["nameSource"]:
			mainptr.nameSourcesSet.Append(option)
	else:
		mainptr.nameSourcesSet.Disable()
		mainptr.nameSourcesSet.Append("Disabled")
		mainptr.nameSourcesSet.SetValue("Disabled")


	mainptr.overwriteMode.Clear()							# And overwriteMode Dropbox
	for option in configDict["overwriteModes"]:
		mainptr.overwriteMode.Append(option)
		mainptr.overwriteMode.Enable()
		#mainptr.overwriteMode.SetSelection(0)



	if configDict["pointer"].version >= 1.2:					# Populate labels with values from plugin (later plugin versions allow this)
		mainptr.nameList.ChangeValue("%s" % (configDict["pointer"].instructionText))
		mainptr.nameListLabel.SetLabel("  %s  " % (configDict["pointer"].aListLabeltxt))

		mainptr.nameList.Fit()
		mainptr.nameListLabel.Fit()


	elif configDict["pointer"].version > 1:
		mainptr.nameListLabel.SetLabel("  %s  " % (configDict["pointer"].aListLabeltxt))
		mainptr.nameListLabel.Fit()


	if configDict["pointer"].version == 1:						# Clear and repopulate Input List field title
		mainptr.nameListLabel.SetLabel("  Artist List File: ")
		mainptr.nameListLabel.Fit()


	mainptr.nameList.Clear()

	mainptr.Layout()
	mainptr.Refresh()
	mainptr.Thaw()


def loadNameFile(filename, mainptr):

	mainptr.nameList.Clear()

	namelistDup = []


	try:
		listhandle = open(filename, "r")
	except:
		return "Failed"				# File does not exist
	nametext = listhandle.read()
	nametext = nametext.lower()
	nametext = nametext.replace(" ", "")
	nameset = nametext.split("\n")
	regxComment = re.compile("^#")			# remove commented out Artistnames

	for name in nameset:
		cleaned = name.strip()
		commentline = regxComment.search(cleaned)
		if cleaned != "" and not commentline:
			namelistDup.append(name.strip())

	namelist = set(namelistDup)
	totalelements = len(namelist)
	windowString = ""

	windowString += "%s%s%s" % ("Total of ", totalelements, " names:\n")
	windowString += "----Begin\n"
	for name in namelist:
		windowString += "	" + name + "\n"
	windowString += "----End\n"

	mainptr.nameList.AppendText(windowString)

	return (namelist)

